<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>index</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="./layui/css/layui.css"  media="all">
    <link rel="shortcut icon" href="./favicon.ico" rel="external nofollow" rel="external nofollow" />

</head>
<body>
<div style="margin-bottom: 5px;">

    <!-- 示例-970 -->
    <ins class="adsbygoogle" style="display:inline-block;width:970px;height:90px" data-ad-client="ca-pub-6111334333458862" data-ad-slot="3820120620"></ins>

</div>

<div class="layui-btn-group demoTable">
    <button class="layui-btn" data-type="getCheckData">删除选中歌曲</button>
<!--    <button class="layui-btn" data-type="getCheckLength">获取选中数目</button>-->
<!--    <button class="layui-btn" data-type="isAll">验证是否全选</button>-->
</div>

<table class="layui-table" lay-data="{width: 892, height:400, url:'/music', page:true, id:'idTest'}" lay-filter="demo">
    <thead>
    <tr>
        <th lay-data="{type:'checkbox', fixed: 'left'}"></th>
        <th lay-data="{field:'id', width:80, sort: true, fixed: true}">ID</th>
        <th lay-data="{field:'name', width:120, sort: true, fixed: true}">歌曲名</th>
        <th lay-data="{field:'singer', width:120, sort: true, fixed: true}">歌手</th>
<!--        <th lay-data="{field:'imgPath', width:120, fixed: true}">图片路径</th>-->
<!--        <th lay-data="{field:'musicPath', width:120, fixed: true}">歌曲路径</th>-->
        <th lay-data="{fixed: 'right', width:178, align:'center', toolbar: '#barDemo'}"></th>
    </tr>
    </thead>
</table>
<!--<div style="width: 500px;height: auto;padding: 10px;margin: 10px">-->
<!--    <div style="width: 50px;height: 50px;float: left;display: inline">-->
<!--        <img id="musicImg" src="./icon/music.png" style="width: 50px;height: 50px;float: left;"/>-->
<!--    </div>-->
<!--    <div style="margin-left: 10px">-->
<!--        <audio id="audioPlayer" controls=""></audio>-->
<!--    </div>-->
<!--</div>-->
<div style="width: 500px;height: auto;padding: 10px;margin: 10px">
    <div style="width: 50px;height: 50px;float: left;display: inline">
    </div>
</div>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">播放</a>
    <a class="layui-btn layui-btn-xs" lay-event="detail" href="/detail.html;">详情</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<script src="./layui/layui.js" charset="utf-8"></script>

<script>
    layui.use(['table'], function(){
        var table = layui.table;
        //监听表格复选框选择
        table.on('checkbox(demo)', function(obj){
            console.log(obj)
        });
        //监听工具条
        table.on('tool(demo)', function(obj){
            var data = obj.data;
            if(obj.event === 'detail'){
                layer.msg('ID：'+ data.id + ' 的播放操作');
                // layui.$("#audioPlayer").attr("src","http://172.26.63.130:8080"+"/mp3/"+data.musicPath);
                // layui.$("#audioPlayer").attr("src","/mp3/"+data.musicPath);
                // layui.$("#musicImg").attr("src","/img/"+data.imgPath);
                // layui.$("#audioPlayer")[0].play();
                // localStorage["playingId"] = 0;
                // localStorage["playingName"] = 0;
                // localStorage["playingSinger"] = 0;
                // localStorage["playingImgPath"] = 0;
                // localStorage["playingMusicPath"] = 0;


                localStorage["playingId"] = data.id;
                localStorage["playingName"] = data.name;
                localStorage["playingSinger"] = data.singer;
                localStorage["playingImgPath"] = data.imgPath;
                localStorage["playingMusicPath"] = data.musicPath;
            } else if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    obj.del();
                    layer.close(index);
                    $.ajax({
                        url:'/music/'+data.id,
                        method:'delete',
                        data:{"id":data.id},
                        dataType:'JSON',
                        contentType: "application/json;charset=utf-8",
                        success:function(res){
                            if(res.code==0){
                                //layer.alert(res.msg);
                            }
                            else
                                layer.alert("失败");
                        }
                    })
                });
            } else if(obj.event === 'edit'){
                layer.alert('编辑行：<br>'+ JSON.stringify(data))
            }
        });

        var $ = layui.$, active = {
            getCheckData: function(){ //删除选中数据
                var checkStatus = table.checkStatus('idTest')
                    ,data = checkStatus.data;
                //console.log(data);
                for(var i in data){
                    var data_id = data[i].id;
                    //console.log(data_id);
                    $.ajax({
                        url:'/music',
                        method:'delete',
                        data:{"id":data_id},
                        dataType:'JSON',
                        contentType: "application/json;charset=utf-8",
                    })
                }
                //layer.alert(JSON.stringify(data));
                location.reload();
            }
            ,getCheckLength: function(){ //获取选中数目
                var checkStatus = table.checkStatus('idTest')
                    ,data = checkStatus.data;
                layer.msg('选中了：'+ data.length + ' 个');
            }
            ,isAll: function(){ //验证是否全选
                var checkStatus = table.checkStatus('idTest');
                layer.msg(checkStatus.isAll ? '全选': '未全选')
            }
        };

        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>

</body>
</html>